// pages/index/government_affairs/government_affairs.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    resultActivity: [],
    options: ['开展活动', '已结束'],
    type:2
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      theme: app.globalData.theme
    });
    app.setNavigationBarColor();
    app.globalData.pageNumber = 1;
    //this.govermmentAffairsLoad();
    this.getYardActivity();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    app.globalData.pageTotalNumber = 0;
    app.globalData.pageNumber = 1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 滚动底部架加载
   */
  handleLoadmore: function() {
    app.globalData.pageNumber++;
    this.setData({
      mpLoadingShow: true,
      mpLoadingTips: "加载中..."
    });
    if (app.globalData.pageNumber <= app.globalData.pageTotalNumber) {
      this.getYardActivity();
    } else {
      setTimeout(() => {
        this.setData({
          mpLoadingTips: "暂无数据"
        });
      }, 500);
      setTimeout(() => {
        this.setData({
          mpLoadingShow: false
        });
      }, 1000);
    }
  },
  /**
   * 切换
   */
  changeType: function(e) {
    let index = e.detail.value;
    app.globalData.pageTotalNumber = 0;
    app.globalData.pageNumber = 1;
    if (index==0){
      index = index+2;
    }
    this.setData({
      resultActivity: [],
      type: index
    });
    this.getYardActivity();
  },
  /**
   * 院坝活动
   */
  getYardActivity: function() {
    app.wxGetRequest("/wechart/life/getMicroservice", {
      area_code: app.globalData.parentAreaCode,
      url: app.globalData.urlMicro + "/v1/integral/activities/mouths",
      type: this.data.type,
      "page.page_number": app.globalData.pageNumber,
      "page.page_size": app.globalData.pageSize,
    }).then(result => {
      let pageTotalNumber = Math.ceil(result.data.count / app.globalData.pageSize);
      this.setData({
        resultActivity: this.data.resultActivity.concat(result.data.records),
        noData: pageTotalNumber == 0 ? true : false,
        mpLoadingShow: false
      });
      app.globalData.pageTotalNumber = pageTotalNumber;
    });
  },
  /**
   * 扫码
   */
  scanning: function() {
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        let id = res.result.split("活动id:")[1];
        app.wxGetRequest("/wechart/life/postMicroservice", {
          area_code: app.globalData.parentAreaCode,
          url: app.globalData.urlMicro + "/v1/integral/activities/qa",
          user_id: app.globalData.userInfo["user_id"],
          id: id
        }).then(result => {
          app.alert("温馨提示", result.data.message);
        });
      }
    })
  },
  /**
   * 详情
   */
  residentDetail: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/resident_activity/resident_activity_scanning/resident_activity_scanning?id=' + id,
    })
  }
})